package com.lushuai.jiubeidanci.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.lushuai.jiubeidanci.R;
import com.lushuai.jiubeidanci.activity.ProfileDetailActivity;
import com.lushuai.jiubeidanci.activity.SettingsActivity;
import com.lushuai.jiubeidanci.dao.UserDao;
import com.lushuai.jiubeidanci.model.User;

public class ProfileFragment extends Fragment {
    private ImageView ivAvatar;
    private TextView tvUserId;
    private TextView tvVipStatus;
    private TextView tvCoins;
    private UserDao userDao;
    private User currentUser;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        
        // 初始化DAO
        userDao = new UserDao(getContext());
        
        // 初始化视图
        initViews(view);
        
        // 加载用户数据
        loadUserData();
        
        // 设置点击事件
        setupClickListeners(view);
        
        return view;
    }

    private void initViews(View view) {
        ivAvatar = view.findViewById(R.id.iv_avatar);
        tvUserId = view.findViewById(R.id.tv_user_id);
        tvVipStatus = view.findViewById(R.id.tv_vip_status);
        tvCoins = view.findViewById(R.id.tv_coins);
    }

    private void loadUserData() {
        // 从SharedPreferences获取当前用户ID
        SharedPreferences sp = getActivity().getSharedPreferences("user_info", getActivity().MODE_PRIVATE);
        long userId = sp.getLong("user_id", -1);
        
        if (userId != -1) {
            currentUser = userDao.getUserById(userId);
            if (currentUser != null) {
                // 设置用户ID
                tvUserId.setText(String.valueOf(currentUser.getNickname()));
                
                // 设置VIP状态
                tvVipStatus.setText(currentUser.isVip() ? "已开通大会员" : "未开通大会员");
                
                // 设置酷币数量
                tvCoins.setText(String.valueOf(currentUser.getCoins()));
                
                // 设置头像（如果有）
                if (currentUser.getAvatarUrl() != null) {
                    // TODO: 使用图片加载库加载头像
                    // Glide.with(this).load(currentUser.getAvatarUrl()).into(ivAvatar);
                }
            }
        }
    }

    private void setupClickListeners(View view) {
        // 用户信息设置点击事件
        view.findViewById(R.id.layout_user_info).setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), ProfileDetailActivity.class);
            if (currentUser != null) {
                intent.putExtra("user_id", currentUser.getUserId());
            }
            startActivity(intent);
        });

        // 设置点击事件
        view.findViewById(R.id.layout_settings).setOnClickListener(v -> {
            startActivity(new Intent(getActivity(), SettingsActivity.class));
        });

        // 其他点击事件...
    }

    @Override
    public void onResume() {
        super.onResume();
        // 每次返回都重新加载用户数据
        loadUserData();
    }
}